package com.niodata.dp.task.dao;

import com.niodata.dp.task.entity.JobDepInfo;
import com.niodata.dp.task.entity.JobExecData;
import com.niodata.dp.task.entity.JobInfo;
import com.niodata.dp.task.entity.JobNodeStyleInfo;
import com.niodata.dp.task.entity.JobParams;
import com.niodata.dp.task.entity.JobState;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;


@Mapper
public interface JobMapper {

  @Insert("insert into JOB_INFO(JOB_ID,PKG_NAME,JOB_NAME,PKG_VERSION,"
      + "PKG_PATH,JOB_TYPE,PKG_TYPE,PKG_LANG,PARAMS,TASK_ID,PKG_ID,NODE_STYLE)"
      + "values(#{job.jobId},#{job.pkgName},#{job.jobName},#{job.pkgVersion},"
      + "#{job.pkgPath},#{job.jobType},#{job.pkgType},#{job.pkgLang},"
      + "#{job.params},#{job.taskId},#{job.pkgId},#{job.nodeStyleInfo})")
  public void saveJobInfo(@Param("job") JobInfo jobInfo);

  @Insert("insert into JOB_DEPS (TASK_ID,JOB_ID,DEP_JOB_ID) values(#{taskId},#{jobId},#{depJobId})")
  public void saveJobDepInfo(@Param("taskId") String taskId,
      @Param("jobId") String jobId, @Param("depJobId") String depJobId);

  @Select("select TASK_ID as taskId,JOB_ID as jobId,"
      + "DEP_JOB_ID as depJobId from JOB_DEPS where TASK_ID=#{taskId}")
  public List<JobDepInfo> getJobDepInfoOfTask(@Param("taskId") String taskId);

  @Select("select job.* from JOB_INFO job,JOB_DEPS deps where job.JOB_ID=deps.DEP_JOB_ID and "
      + "deps.JOB_ID=#{jobId}")
  @ResultMap("jobInfoResultMap")
  public List<JobInfo> getDepJobs(@Param("jobId") String jobId);

  @Select("select * from JOB_INFO where TASK_ID=#{taskid}")
  @ResultMap("jobInfoResultMap")
  public List<JobInfo> getJobsOfTask(@Param("taskid") String taskId);

  @Select("select ji.* from JOB_INFO ji inner join TASK_INFO ti ON ji.TASK_ID = ti.TASK_ID "
      + " where ti.TASK_NAME = #{taskname}")
  @ResultMap("jobInfoResultMap")
  public List<JobInfo> getJobsOfTaskByName(@Param("taskname") String taskname);

  @Delete("delete from JOB_INFO where TASK_ID=#{taskid}")
  public void deleteJobTasks(@Param("taskid") String taskId);

  @Delete("delete from JOB_DEPS where TASK_ID=#{taskid}")
  public void deleteJobDeps(@Param("taskid") String taskId);

  @Select("select * from JOB_INFO where id=#{id}")
  @ResultMap("jobInfoResultMap")
  public JobInfo getById(@Param("id") String id);

  @Select("select * from JOB_INFO where DEP_JOB=#{jobid}")
  @ResultMap("jobInfoResultMap")
  public JobInfo getPostJob(@Param("jobid") String jobid);

  @Select("select job.* from JOB_INFO job,TASK_INFO task  where job.TASK_ID =task.TASK_ID and "
      + "job.JOB_NAME=#{jobname} and task.TASK_NAME=#{taskname}")
  @ResultMap("jobInfoResultMap")
  public JobInfo getJobByName(@Param("jobname") String jobname, @Param("taskname") String taskName);

  @Select("select job.* from JOB_INFO job  where job.TASK_ID =#{taskId} and "
      + "job.JOB_NAME=#{jobname}")
  @ResultMap("jobInfoResultMap")
  public JobInfo getJobByNameByTaskId(@Param("jobname") String jobname,
      @Param("taskId") String taskid);

  @Update("update JOB_INFO set PARAMS=#{params} where JOB_ID=#{id}")
  public void updateJobParams(@Param("id") String id, @Param("params") JobParams jobParams);

  @Update("update JOB_INFO set PKG_ID = #{jobInfo.pkgId}, PKG_VERSION=#{jobInfo.pkgVersion},"
      + "PKG_PATH = #{jobInfo.pkgPath},PKG_TYPE = #{jobInfo.pkgType},"
      + "PKG_LANG = #{jobInfo.pkgLang} where JOB_ID=#{jobInfo.jobId}")
  public void updateJobPkgInfo(@Param("jobInfo") JobInfo jobInfo);

  @Update("update JOB_INFO set NODE_STYLE=#{nodeStyle} where JOB_ID=#{id}")
  public void updateJobNodeStyle(@Param("id") String id,
      @Param("nodeStyle") JobNodeStyleInfo nodeStyle);


  @Select("select JOB_NAME as jobName,"
      + "TASK_NAME as taskName,TASK_ID as taskId,JOB_ID as jobId,EXEC_ID as execId,"
      + "PARAMS as params,OUT_PARAMS as outParams "
      + "from JOB_EXEC_DATA where  JOB_NAME=#{jobName} and EXEC_ID=#{execId}")
  public JobExecData getJobExecData(@Param("execId") int execId, @Param("jobName") String jobName);

  @Insert(
      "insert into JOB_EXEC_DATA (JOB_NAME,TASK_NAME,TASK_ID,"
          + "JOB_ID,EXEC_ID,PARAMS,OUT_PARAMS)VALUES"
          + "(#{exec.jobName},#{exec.taskName},#{exec.taskId},"
          + "#{exec.jobId},#{exec.execId},#{exec.params},#{exec.outParams})")
  public void saveJobExecData(@Param("exec") JobExecData exec);

  @Delete("delete from JOB_EXEC_DATA where EXEC_ID=#{execId}")
  public void deleteJobExecData(@Param("execId") int execId);

  @Update("update JOB_EXEC_DATA set PARAMS=#{exec.params},OUT_PARAMS=#{exec.outParams} "
      + "where JOB_ID=#{exec.jobId}  "
      + "and TASK_ID=#{exec.taskId}  "
      + "and EXEC_ID=#{exec.execId}  "
      + "and JOB_NAME=#{exec.jobName}")
  public void updateJobExecData(@Param("exec") JobExecData exec);

  @Select("select JOB_NAME as jobName,"
      + "TASK_NAME as taskName,TASK_ID as taskId,JOB_ID as jobId,EXEC_ID as execId,"
      + "PARAMS as params,OUT_PARAMS as outParams "
      + "from JOB_EXEC_DATA where EXEC_ID=#{execId}")
  public List<JobExecData> getJobDataOfExec(@Param("execId") int execId);

  @Select("select TASK_NAME as taskName"
      + ",JOB_NAME as jobName,SCHEDULE_ID as scheduleId,"
      + "STATE as state from JOB_STATE"
      + " where TASK_NAME=#{taskName}"
      + " and JOB_NAME=#{jobName}"
      + " and SCHEDULE_ID=#{scheduleId}")
  public JobState getJobState(@Param("taskName") String taskName, @Param("jobName") String jobName,
      @Param("scheduleId") String scheduleId);

  @Insert("insert into JOB_STATE(TASK_NAME,JOB_NAME,SCHEDULE_ID,STATE)"
      + " values(#{jobState.taskName},#{jobState.jobName},"
      + "#{jobState.scheduleId},#{jobState.state})")
  public void saveJobState(@Param("jobState") JobState jobState);

  @Update("update JOB_STATE set STATE=#{jobState.state}"
      + " where TASK_NAME=#{jobState.taskName}"
      + " and JOB_NAME=#{jobState.jobName}"
      + " and SCHEDULE_ID=#{jobState.scheduleId}")
  public void updateJobState(@Param("jobState") JobState jobState);

  @Select("select case when exists (select 1 from JOB_INFO"
      + " where PKG_ID=#{pkgId}) then 1 else 0 end")
  public boolean existsPlugin(@Param("pkgId") String pkgId);

  @Select("select count(1) as job_count from JOB_INFO WHERE PKG_ID=#{pkgId}")
  public int getPluginJobCount(@Param("pkgId") String pkgId);
}
